package com.movile.checkhoras.android.widget;

import java.util.Calendar;

import com.movile.checkhoras.android.R;
import com.movile.checkhoras.android.dao.TableCheckHorasHelper;
import com.movile.checkhoras.android.vo.CheckHora;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;

import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;


public class UpdateWidgetService extends Service {

    TableCheckHorasHelper tableCheckHoras;
    
    @Override
    public void onStart(Intent intent, int startId) {
        
      
        Bundle params = intent.getExtras();
        int idWidget = params.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);               
        
        RemoteViews rv = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_1_layout);
        ComponentName thisWidget = new ComponentName(this, CheckWidget1Provider.class);                
        
        updateIntents(rv, idWidget);
        
        tableCheckHoras = new TableCheckHorasHelper(getBaseContext());
        
        Calendar cal = Calendar.getInstance();
        cal.roll(Calendar.DATE, false);                
        int mes = cal.get(Calendar.MONTH) + 1;
        int dia = cal.get(Calendar.DAY_OF_MONTH) + 1;

         CheckHora check = new CheckHora();            
         
         check.setAno(cal.get(Calendar.YEAR));
         check.setDataString(cal.get(Calendar.YEAR) + "-" + mes + "-" + dia + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
         check.setDia(cal.get(Calendar.DAY_OF_MONTH) + 1);
         check.setHora(cal.get(Calendar.HOUR_OF_DAY));
         check.setMinuto(cal.get(Calendar.MINUTE));
         check.setMes(mes);
         check.setMilisegundos(cal.getTime().getTime());
                         
                 
         insertAndUpdateCheck(rv, check);
         
               
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
        
        //appWidgetManager.getAppWidgetIds(thisWidget);
        
        appWidgetManager.updateAppWidget(thisWidget, rv);
        
        onDestroy();                
                      
    }

    @Override
    public IBinder onBind(Intent arg0) {
       
        
        return null;
    }
    
    
    private void updateIntents(RemoteViews rv, int idWidget){
        
        
        Intent intent = new Intent(getBaseContext(),UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,idWidget);            
        PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), idWidget, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        
        rv.setOnClickPendingIntent(R.id.btWidgetCheck, pendingIntent);
                                                
      //  appWidgetManager.updateAppWidget(appWidgetId, views);
        
    }
    
    private void insertAndUpdateCheck(RemoteViews views, CheckHora check){
        if(tableCheckHoras.IsCheckInLast()){
            Log.i("updateCheck", "CheckIN");
            check.setCheckIn(false);
            tableCheckHoras.insert(check);
            
            views.setTextViewText(R.id.txtWidgetCheck, "CheckIn");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkinwid);
                        
        } else {
            Log.i("updateCheck", "CheckOUT");
            check.setCheckIn(true);
            tableCheckHoras.insert(check);
            
            views.setTextViewText(R.id.txtWidgetCheck, "CheckOut");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkoutwid);           
        }
    }

}
